//
//  MeModifyViewController.m
//  DBAChain
//
//  Created by Mxionlly on 2018/9/9.
//  Copyright © 2018年 Evan. All rights reserved.
//

#import "MeModifyViewController.h"
#import "taskUserManagerService.h"

@interface MeModifyViewController ()
@property (weak, nonatomic) IBOutlet UIButton *codeButton;
@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UITextField *passField;
@property (weak, nonatomic) IBOutlet UITextField *passField1;
@property (weak, nonatomic) IBOutlet UITextField *codeField;
@property (nonatomic,assign)NSInteger type;  /**< 0：修改登录 1：修改支付    */
@property (nonatomic, strong) NSTimer           *timer;
@property (nonatomic, assign) NSInteger         number;

///短信收到的验证码
@property (nonatomic, strong) NSString*         verifyCode;
@end

@implementation MeModifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = self.type == 0 ? @"修改登录密码" : @"修改支付密码";
   
    self.number = 60;
    self.phoneField.text = [UserCenter shareCenter].user.phone;
    [self initCodeView:@"发送验证码"];
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSliderAction)]];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
     [self setBlackStatus:YES];
}

- (id)initWithType:(NSInteger )type {
    self = [super init];
    if (self) {
        _type = type;
    }
    return self;
}

- (IBAction)getCodeAction:(id)sender {
    if (![Check isMobileNumber:self.phoneField.text]) {
        [DKProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
        return;
    }else{
        [DKProgressHUD showLoadingWithStatus:@"发送中"];
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setField:[UserCenter shareCenter].user.phone value:@"phone"];
        [dict setField:[UserCenter shareCenter].user.pid value:@"uid"];
        [taskUserManagerService getVerifyCode:dict success:^(dataGetVerifyCode *response, BOOL success) {
            [DKProgressHUD dismiss];
            if (success) {
                self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updataAction) userInfo:nil repeats:YES];
                self.codeButton.userInteractionEnabled = NO;
                self.verifyCode = response.data;
                return ;
            }
            [DKProgressHUD showErrorWithStatus:@"发送失败"];
        } failture:^(NSError *error, NSString *strDescription) {
            NSString *strErr = [error.userInfo objectForKey:@"NSLocalizedDescription"];
            [DKProgressHUD showErrorWithStatus:strErr];
        }];
    }
}

- (void)updataAction {
    self.number--;
    [self initCodeView:[NSString stringWithFormat:@"%lds",(long)self.number]];
    if (self.number <= 0) {
        [self closeTimer];
    }
}

- (void)closeTimer {
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
        self.number = 60;
        self.codeButton.userInteractionEnabled = YES;
        [self initCodeView:@"发送验证码"];
    }
}

-(void)initCodeView:(NSString *)title
{
    if ([title isEqualToString:@"发送验证码"]) {
        [self.codeButton setTitle:title forState:UIControlStateNormal];
        [self.codeButton setTitleColor:COLOR_main forState:UIControlStateNormal];
    }else{
//        NSMutableAttributedString* tncString = [[NSMutableAttributedString alloc] initWithString:title];
//        [tncString addAttribute:NSUnderlineStyleAttributeName
//                          value:@(NSUnderlineStyleSingle)
//                          range:(NSRange){0,[tncString length]}];
//        [tncString addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor]  range:NSMakeRange(0,[tncString length])];
//        [tncString addAttribute:NSUnderlineColorAttributeName value:[UIColor grayColor] range:(NSRange){0,[tncString length]}];
//        [self.codeButton setAttributedTitle:tncString forState:UIControlStateNormal];
        [self.codeButton setTitle:title forState:UIControlStateNormal];
        [self.codeButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    }
}

- (IBAction)modifyAction:(id)sender {
    if (![Check isMobileNumber:self.phoneField.text]) {
        [DKProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
        return;
    }
    if (![Check checkLenght:self.codeField.text]) {
        [DKProgressHUD showErrorWithStatus:@"请输入验证码"];
        return;
    }
    if (![Check checkLenght:self.passField.text] || ![Check checkLenght:self.passField1.text]) {
        [DKProgressHUD showErrorWithStatus:@"请设置密码"];
        return;
    }
    if (![self.passField.text isEqualToString:self.passField1.text]) {
        [DKProgressHUD showErrorWithStatus:@"两次密码设置不一致"];
        return;
    }
    if (self.codeField.text != self.verifyCode) {
        [DKProgressHUD showErrorWithStatus:@"您输入的验证码有误"];
        return;
    }
    [DKProgressHUD showLoadingWithStatus:@"加载中..."];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setField:self.codeField.text value:@"verifyCode"];
    
    if (self.type == 0) {
        [dict setField:self.passField1.text value:@"newpwd"];
        [taskUserManagerService getChangePwd:dict success:^(BaseModel *response, BOOL success) {
            [DKProgressHUD dismiss];
            if (success) {
                [[UserCenter shareCenter] refreshProfiles:^(dataGetUserInfo *profile) {
                    
                }];
                [DKProgressHUD showErrorWithStatus:@"修改成功"];
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [DKProgressHUD showErrorWithStatus:response.message];
            }
        } failture:^(NSError *error, NSString *strDescription) {
            [DKProgressHUD showErrorWithStatus:strDescription];
        }];
    }else{
        [dict setField:self.passField1.text value:@"newwithpwd"];
        [taskUserManagerService getSetPayPassword:dict success:^(BaseModel *response, BOOL success) {
            [DKProgressHUD dismiss];
            if (success) {
                [[UserCenter shareCenter] refreshProfiles:^(dataGetUserInfo *profile) {
                    
                }];
                [DKProgressHUD showErrorWithStatus:@"修改成功"];
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [DKProgressHUD showErrorWithStatus:response.message];
            }
        } failture:^(NSError *error, NSString *strDescription) {
            [DKProgressHUD showErrorWithStatus:strDescription];
        }];
    }
}

-(void)tapSliderAction
{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
